package com.data.modules.online.cgform.service.impl;

import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.data.common.online.api.IOnlineBaseExtAPI;
import com.data.common.system.vo.DictModel;
import com.data.modules.online.cgform.util.CgformUtil;
import com.data.modules.online.cgform.service.IOnlCgformHeadService;
import com.data.modules.online.cgreport.common.CgReportConstant;
import com.data.modules.online.cgreport.service.IOnlCgreportAPIService;
import com.data.modules.online.config.exception.DBException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OnlineBaseExtAPI implements IOnlineBaseExtAPI {

    @Autowired
    @Lazy(true)
    private IOnlCgformHeadService onlCgformHeadService;
    @Autowired
    IOnlCgreportAPIService onlCgreportAPIService;

    @Override
    public String cgformPostCrazyForm(String tableName, JSONObject jsonObject) throws Exception {
        String a = CgformUtil.getId();
        jsonObject.put("id", a);
        this.onlCgformHeadService.addCrazyFormData(tableName, jsonObject);
        return a;
    }

    @Override
    public String cgformPutCrazyForm(String tableName, JSONObject jsonObject) throws Exception {
        jsonObject.remove("create_by");
        jsonObject.remove("create_time");
        jsonObject.remove("update_by");
        jsonObject.remove("update_time");
        this.onlCgformHeadService.editCrazyFormData(tableName, jsonObject);
        return jsonObject.getString("id");
    }

    @Override
    public String cgformDeleteDataByCode(String cgformCode, String dataIds) {
        return this.onlCgformHeadService.deleteDataByCode(cgformCode, dataIds);
    }

    @Override
    public JSONObject cgformQueryAllDataByTableName(String tableName, String dataIds) {
        try {
            return this.onlCgformHeadService.queryAllDataByTableNameForDesform(tableName, dataIds);
        } catch (DBException e) {
            log.error("查询主子表数据失败：", e);
            return null;
        }
    }

    @Override
    public Map<String, Object> cgreportGetData(String code, String forceKey, String dataList) {
        HashMap hashMap = new HashMap();
        hashMap.put(forceKey, dataList);
        hashMap.put("getAll", true);
        return this.onlCgreportAPIService.getDataByCode(code, hashMap);
    }

    @Override
    public List<DictModel> cgreportGetDataPackage(String code, String dictText, String dictCode, String dataList) {
        ArrayList arrayList = new ArrayList();
        for (Object map : (List) cgreportGetData(code, CgReportConstant.f351E + dictCode, dataList).get("records")) {
            HashMap tmp = (HashMap) map;
            arrayList.add(new DictModel((String) tmp.get(dictCode), (String) tmp.get(dictText)));
        }

        /*for (Map map : (List) cgreportGetData(code, CgReportConstant.f351E + dictCode, dataList).get("records")) {
            arrayList.add(new DictModel((String) map.get(dictCode), (String) map.get(dictText)));
        }*/
        return arrayList;
    }
}
